package common

import (
	"bytes"
	"fmt"
	"github.com/spf13/viper"
	"github.com/toolkits/pkg/file"
	"hera-admin/pkg/iconf"
)

type Server struct {
	Zap   Zap   `mapstructure:"zap"    json:"zap" yaml:"zap"`
	Mysql Mysql `mapstructure:"mysql"  json:"mysql" yaml:"mysql"`
}

//var Config *ConfigStruct
func Parse() error {
	ymlFile := iconf.GetYmlFile("server")
	if ymlFile == "" {
		return fmt.Errorf("configuration file of server not found")
	}

	bs, err := file.ReadBytes(ymlFile)
	if err != nil {
		return fmt.Errorf("cannot read yml[%s]: %v", ymlFile, err)
	}

	viper.SetConfigType("yaml")
	err = viper.ReadConfig(bytes.NewBuffer(bs))
	if err != nil {
		return fmt.Errorf("cannot read yml[%s]: %v", ymlFile, err)
	}

	fmt.Println("config.file:", ymlFile)

	return nil
}
